import URL from './url.js'
import base64 from 'base-64'

export default class User {
    constructor(num, password) {
        this.num = num;
        this.password = password;
        //this.num = 'yx101';
        //this.password = 'yx101';
    }

    gen_get_url(url, params) {
        if (params) {
            let paramsArray = []
            Object.keys(params).forEach(key => paramsArray.push(key + '=' + encodeURIComponent(params[key])))
            if (url.search(/\?/) === -1) {
                url += '?' + paramsArray.join('&')
            } else {
                url += '&' + paramsArray.join('&')
            }
            return url;
        }
    }

    async do_login() {
        try {
            let response = await fetch(URL.LOGIN_URL, {
                method: 'GET',
                headers: {
                    'x_domainid': '3',
                    'x_account': 'bks',
                    'x_secret': 'YmtzQDIwMTMwNjI1',
                    'u_username': this.num,
                    'u_password': base64.encode(this.password)
                }
            });
            let responseJson = await response.json();
            return responseJson;
        } catch (error) {
            console.log("User", error);
        }
        return null;
    }
}